<template>
  <div class="address">
    <scroll-view scroll-y="true" class="addcont" style="height:100%">
      <div class="item" v-if="listData.length!=0">
        <div class="list" v-for="(item, index) in listData" :key="index">
          <div class="addresslist">
            <div>
              <span>{{item.name}}</span>
              <div class="moren" v-if="item.is_default==1">默认</div>
            </div>
            <div class="info" @click="selAddress(item.id)">
              <p>{{item.mobile}}</p>
              <p>{{item.address+item.address_detail}}</p>
            </div>
            <div @click="toDetail(item.id)"></div>
          </div>
        </div>
      </div>
      <div class="center" v-else>
        <p>收货地址在哪里？</p>
      </div>
    </scroll-view>

    <div class="bottom">
      <div @click="wxaddress(1)">+新建地址</div>
      <div @click="wxaddress">一键导入微信地址</div>
    </div>
  </div>
</template>

<script>
import { get, getStorageOpenId } from "../../utils/index";
export default {
  data() {
    return {
      listData: [],
      openId: ""
    };
  },
  //! 注意，这里如果用mounted会出bug，跳转回来的时候页面不会自动刷新，必须用onShow
  onShow() {
    this.openId = getStorageOpenId();
    this.getAddressList();
  },
  methods: {
    //. url中的id要与形参中的id保持一致 这个方法是编辑地址的
    toDetail(id) {
      wx.navigateTo({
          url: "/pages/addaddress/main?id="+id,
        });
        // console.log(id);
    },
    wxaddress(index) {
      if (index === 1) {
        wx.navigateTo({
          url: "/pages/addaddress/main",
          success: result => {},
          fail: () => {},
          complete: () => {}
        });
        //~一键导入微信地址（这个有点毛病，点不了）
      } else {
        wx.chooseAddress({
          success: result => {
            let res = encodeURIComponent(JSON.stringify(result));
            wx.navigateTo({
              url: "/pages/addaddress/main?res="+res,
            });
          },
          fail: () => {},
          complete: () => {}
        });
      }
    },
    async getAddressList() {
      let _this = this;
      const data = await get("/address/getListAction", {
        openId: this.openId
      });
      console.log(data);
      this.listData = data.data;
    },
    selAddress(id){
      wx.setStorageSync('addressId',id);
      wx.navigateBack({
        delta: 1
      });
    }
  }
};
</script>

<style lang="less" scoped>
@import url("./style.less");
</style>